﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;

namespace PhotoIDScanner
{
    public partial class frmSettings : Form
    {
        public frmSettings()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (Directory.Exists(txtFilePath.Text))
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;

                XmlWriter writer = XmlWriter.Create("settings.xml", settings);
                writer.WriteStartDocument();
                writer.WriteComment("This file is generated by PhotoIDScanner.");
                writer.WriteStartElement("Settings");
                writer.WriteElementString("sharePath", txtFilePath.Text);
                writer.WriteEndElement();
                writer.WriteEndDocument();

                writer.Flush();
                writer.Close();

                MessageBox.Show("All IDs will be saved to " + txtFilePath.Text, "Settings Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else { MessageBox.Show("The path is invalid or you do not have permissions.  Check the file path and permission.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand); }
        }

        private void frmSettings_Load(object sender, EventArgs e)
        {
            if (File.Exists("settings.xml"))
            {
                XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.
                xmlDoc.Load("settings.xml"); //* load the XML document from the specified file.

                //* Get elements.
                XmlNodeList uncPath = xmlDoc.GetElementsByTagName("sharePath");

                //* Display the results.
                txtFilePath.Text = uncPath[0].InnerText;
            }

        }
    }
}
